<template>
  <a-modal v-model:visible="visible" :title="title" :mask-closable="false" :esc-to-close="false"
    :width="width >= 600 ? 600 : '100%'" draggable @before-ok="save" @close="reset">
    <GiForm ref="formRef" v-model="form" :columns="columns" />
  </a-modal>
</template>

<script setup lang="ts">
  import { Message } from '@arco-design/web-vue'
  import { useWindowSize } from '@vueuse/core'
  import { getCheckTask, addCheckTask, updateCheckTask } from '@/apis/asset/check/task'
  import { type ColumnItem, GiForm } from '@/components/GiForm'
  import { useResetReactive } from '@/hooks'
  import { useDict } from '@/hooks/app'
  import { listUserDict } from '@/apis'

  const emit = defineEmits < {
  (e: 'save-success'): void
}> ()

  const { width } = useWindowSize()

  const dataId = ref('')
  const visible = ref(false)
  const isUpdate = computed(() => !!dataId.value)
  const title = computed(() => (isUpdate.value ? '修改盘点任务' : '新增盘点任务'))
  const formRef = ref < InstanceType < typeof GiForm >> ()

  const [form, resetForm] = useResetReactive({
    // todo 待补充
  })

  const userList = ref < LabelValueState[] > ([]);
  // 获取用户数据
  const getUserList = async () => {
    const queryData = { status: 1 }
    const { data } = await listUserDict(queryData)
    userList.value = data.map((item) => ({ ...item, value: `${item.value}` }))
  }

  // 项目
  const typeData = [{
    value: 0,
    label: '设备',
  }, {
    value: 1,
    label: '配件',
  }, {
    value: 2,
    label: '软件',
  }]

  const columns: ColumnItem[] = reactive([
    {
      label: '任务名称',
      field: 'name',
      type: 'input',
      span: 24,
      required: true,
    },
    {
      label: '项目',
      field: 'type',
      type: 'select',
      span: 24,
      required: true,
      props: {
        options: typeData,
        placeholder: "请选择项目",
        allowClear: true,
        allowSearch: true,
      }
    },
    {
      label: '开始日期',
      field: 'startDate',
      type: 'date-picker',
      span: 24,
      required: true,
    },
    {
      label: '结束日期',
      field: 'endDate',
      type: 'date-picker',
      span: 24,
      required: true,
    },
    {
      label: '负责人',
      field: 'userId',
      type: 'select',
      span: 24,
      required: true,
      props: {
        options: userList,
        placeholder: "请选择负责人",
        allowClear: true,
        allowSearch: true,
        allowCreate: true,
      },
    },
  ])

  // 重置
  const reset = () => {
    formRef.value?.formRef?.resetFields()
    resetForm()
  }

  // 保存
  const save = async () => {
    try {
      const isInvalid = await formRef.value?.formRef?.validate()
      if (isInvalid) return false
      if (isUpdate.value) {
        await updateCheckTask(form, dataId.value)
        Message.success('修改成功')
      } else {
        await addCheckTask(form)
        Message.success('新增成功')
      }
      emit('save-success')
      return true
    } catch (error) {
      return false
    }
  }

  // 新增
  const onAdd = async () => {
    reset()
    getUserList()
    dataId.value = ''
    visible.value = true
  }

  // 修改
  const onUpdate = async (id: string) => {
    reset()
    getUserList()
    dataId.value = id
    const { data } = await getCheckTask(id)
    Object.assign(form, data)
    visible.value = true
  }

  defineExpose({ onAdd, onUpdate })
</script>

<style scoped lang="scss"></style>